package com.partsoft.dits.mq;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.util.StringUtils;

import com.partsoft.dits.IntegrateDescriptor;

public class MQPoolDescriptor implements IntegrateDescriptor, BeanNameAware {
	
	private String id;
	
	private String defaultQMName;
	
	private String hostname;
	
	private int port = 1414;
	
	private String channelName = "SYSTEM.DEF.SVRCONN";
	
	private String userId = null;

	private String password = null;
	
	private int maxActive = 15;
	
	private int maxIdle = 5;
	
	private int minIdle = 1;
	
	private boolean testOnBorrow = true;
	
	private boolean testOnReturn = false;
	
	private boolean testWhileIdle = false;
	
	private String title;
	
	private String description;
	
	private int ordinal = 0;
	
	public MQPoolDescriptor() {}
	
	public MQPoolDescriptor(String id) {
		setId(id);
	}
	
	public String getTitle() {
		return title == null ? getDefaultQMName() : title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description == null ? "" : description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getDefaultQMName() {
		return defaultQMName;
	}

	public void setDefaultQMName(String defaultQMName) {
		this.defaultQMName = defaultQMName;
	}

	public String getHostname() {
		return hostname;
	}

	public void setHostname(String hostname) {
		this.hostname = hostname;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getChannelName() {
		return channelName;
	}

	public void setChannelName(String channelName) {
		this.channelName = channelName;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public int getMaxActive() {
		return maxActive;
	}

	public void setMaxActive(int maxActive) {
		this.maxActive = maxActive;
	}

	public int getMaxIdle() {
		return maxIdle;
	}
	
	public int getOrdinal() {
		return ordinal;
	}

	public void setOrdinal(int ordinal) {
		this.ordinal = ordinal;
	}

	public void setMaxIdle(int maxIdle) {
		this.maxIdle = maxIdle;
	}

	public int getMinIdle() {
		return minIdle;
	}

	public void setMinIdle(int minIdle) {
		this.minIdle = minIdle;
	}

	public boolean isTestOnBorrow() {
		return testOnBorrow;
	}

	public void setTestOnBorrow(boolean testOnBorrow) {
		this.testOnBorrow = testOnBorrow;
	}

	public boolean isTestOnReturn() {
		return testOnReturn;
	}

	public void setTestOnReturn(boolean testOnReturn) {
		this.testOnReturn = testOnReturn;
	}

	public boolean isTestWhileIdle() {
		return testWhileIdle;
	}

	public void setTestWhileIdle(boolean testWhileIdle) {
		this.testWhileIdle = testWhileIdle;
	}

	/**
	 * 是否远程绑定MQ
	 */
	public boolean isRemoteBind() {
		return StringUtils.hasText(getHostname());
	}

	/**
	 * 是否本地绑定MQ，如果是请把MQ的运行时库路径加入classpath。
	 */
	public boolean isLocalBind() {
		return !isRemoteBind();
	}

	public MQPoolDescriptor clone() {
		try {
			return (MQPoolDescriptor) super.clone();
		} catch (CloneNotSupportedException e) {
			return null;
		}
	}
	
	public void setBeanName(String beanName) {
		setId(beanName);
	}
	

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		MQPoolDescriptor other = (MQPoolDescriptor) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return String.format("MQ连接(%s)", getId());
	}


}
